import {
  queryList,
  queryUbizTpl,
  queryScadaTpl,
} from '../services/index';

export default {
    namespace: 'monitor',
  
    state: {

    },
  
    effects: {
      *queryList({ payload: params }, { call, put }) {
        const { value, ...payload } = params;
        // 解构 type，返回结果拼接
        const data = yield call(queryList, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`ListResource${value}`]: other.Response.Data[0],
              [`ListLoading${value}`]: false,
            },
          })
        } else {
          throw data
        }
      },
      *queryUbizTpl({ payload: params }, { call, put }) {
        const { value, ...payload } = params;
        const data = yield call(queryUbizTpl, payload)
        const { success, message, status, ...other } = data
        if (success) {
          // effects里return 一下你的值, dispatch({}).then((data) => { })
          return other.Response.rows[0]
        } else {
          throw data
        }
      },
      *queryScadaTpl({ payload: params }, { call, put }) {
        const { value, ...payload } = params;
        const data = yield call(queryScadaTpl, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`ScadaTplResource${value}`]: other.Response.rows,
              [`ScadaTplLoading${value}`]: false,
            },
          })
        } else {
          throw data
        }
      },
    },
  
    reducers: {
      setState(state, action) {
        return {
            ...state,
            ...action.payload
        };
      }
    },
  }
  